const axios = require('axios')
const { v4: uuidv4 } = require('uuid')
const randomAuth = new Date().getTime().toString().slice(5)

const options = {
  container: 'lucky-sheet',
  title: '环保执行守法情况',
  lang: 'zh',
  row: 1000,
  column: 64,
  gridKey: 'd87189d9-52f9-aa09-df7b-83d0fbe6684e',
  loadUrl: 'http://81.71.126.122:39999/api/get_load_data/',
  updateUrl: `ws://81.71.126.122:39999/sheet/${randomAuth}/`,
  allowUpdate: true,
  data: [
    {
      name: '',
      order: 0,
      index: 0,
      celldata: [],
      config: {}
    }
  ],
  hook: {},
  cellRightClickConfig: {
    copy: false,
    copyAs: false,
    paste: false,
    insertRow: false,
    insertColumn: false,
    deleteRow: false,
    deleteColumn: false,
    deleteCell: false,
    clear: false,
    matrix: false,
    sort: false,
    filter: false,
    chart: false,
    image: false,
    link: false,
    data: false,
    cellFormat: false,
    customs: [{
      title: '文件上传',
      onClick: (event, pointEvent, {
        rowIndex,
        columnIndex
      }) => {
        console.log(location)
        const input = document.getElementById('hidden_file')
        input.onchange = (e) => {
          const file = e.target.files[0]
          if (file !== null) {
            axios.post('http://81.71.126.122:39999/api/get_file_auth/', {
              gridKey: options.gridKey
            }).then(resp => {
              const { data: { supply } } = resp
              if (supply === '腾讯云') {
                const {
                  data: {
                    expiredTime,
                    credentials: {
                      sessionToken,
                      tmpSecretId,
                      tmpSecretKey
                    },
                    startTime,
                    bucket,
                    region
                  }
                } = resp
                const Cos = require('cos-js-sdk-v5')
                const cos = new Cos({
                  getAuthorization: (options, callback) => {
                    // eslint-disable-next-line node/no-callback-literal
                    callback({
                      TmpSecretId: tmpSecretId,
                      TmpSecretKey: tmpSecretKey,
                      SecurityToken: sessionToken,
                      StartTime: startTime,
                      ExpiredTime: expiredTime
                    })
                  }
                })
                const nowTime = new Date()
                cos.putObject({
                  Bucket: bucket,
                  Region: region,
                  Key: `/uploads/${nowTime.getFullYear()}/${nowTime.getMonth() + 1}/${nowTime.getDate()}/${uuidv4()}.${file.name.split('.').pop().toLowerCase()}`,
                  StorageClass: 'STANDARD',
                  Body: file,
                  onProgress: progressData => {
                    console.log(JSON.stringify(progressData))
                  }
                }, (err, result) => {
                  if (!err) {
                    luckysheet.setCellValue(rowIndex, columnIndex, `https://${result.Location}`)
                  }
                })
              }
            })
          }
        }
        input.click()
      }
    }]
  }
}

const ws = new WebSocket(`ws://81.71.126.122:39999/auth/${randomAuth}/`)
ws.onopen = (event) => {
  console.log(event)
  ws.send('hello!')
}
ws.onmessage = (msg) => {
  console.log(msg.data)
}

console.log(randomAuth)

export default options
